<template>
  <div>
    <!-- 顶部：分类 搜索 增加 导出 -->
    <div class="filter-container">
      <el-select v-model="query.categoryId" placeholder="选择分类" @change="handleSearch">
        <el-option v-for="item in categoryList" :key="item.id" :label="item.name" :value="item.id"></el-option>
      </el-select>

      <el-input v-model="query.keyword" placeholder="输入搜索" style="width: 140px"
                @keyup.enter.native="handleSearch"></el-input>
      <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      <el-button type="primary" style="margin-left: 10px;" icon="el-icon-edit" @click="handleAdd">增加</el-button>
      <el-button :loading="exportLoading" type="primary" icon="el-icon-download" @click="handleExport">导出</el-button>
    </div>

    <!-- 数据列表 编辑 删除 -->
    <div class="list-container">
      <el-table :data="dataList" border fit highlight-current-row>
        <el-table-column label="ID" prop="id" width="55" align="center"></el-table-column>
        <el-table-column label="商品名" prop="name" width="140"></el-table-column>
        <el-table-column label="商品规格" prop="spec"></el-table-column>
        <el-table-column label="单价" width="80">
          <template slot-scope="scope">
            ￥{{ scope.row.price }}
          </template>
        </el-table-column>
        <el-table-column label="库存数" prop="count" align="right" width="80"></el-table-column>

        <el-table-column label="图片" align="center" width="80">
          <template slot-scope="scope">
            <el-image :src="$fullUrl(scope.row.img)" :preview-src-list="[$fullUrl(scope.row.img)]"></el-image>
          </template>
        </el-table-column>

        <el-table-column label="上架时间" prop="createTime" align="center" width="160"></el-table-column>
        <el-table-column label="状态" prop="status" align="center" width="80">
          <template slot-scope="{ row }">
            {{ row.status == 1 ? '在售' : '下架' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="240" align="center">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" @click="handleUpdate(scope.row.id)">编辑</el-button>
            <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="totalCount"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <goods-edit v-if="editDialogVisible" ref="editDialog" @refreshDataList="loadDataList" :categoryId="query.categoryId"
                :categoryList="categoryList"></goods-edit>
  </div>
</template>

<script>
import * as GoodsAPI from '@/api/goods.js';
import * as CategoryAPI from '@/api/category.js';
import GoodsEdit from './components/GoodsEdit.vue';

let dayjs = require('dayjs');
export default {
  name: 'goodsList',
  components: {
    GoodsEdit
  },
  data() {
    return {
      dataList: [], //列表数据
      categoryList: [], //分类选项数据
      query: {
        categoryId: 0,
        keyword: '',
        pageIndex: 1,
        pageSize: 10
      },
      totalCount: 0,

      //导出,还没实现
      exportLoading: false,

      //编辑框是否显示
      editDialogVisible: false
    };
  },

  created() {
    // 获取分类信息数据，初始分类选择组件数据
    CategoryAPI.getList().then(result => {
      this.categoryList = result.data; //返回数据结构参见API文档
      if (this.categoryList.length > 0) {
        this.query.categoryId = this.categoryList[0].id;
        this.loadDataList(); // 初始列表数据
      }
    });
  },

  methods: {
    // 获取列表数据
    loadDataList() {
      GoodsAPI.getPage(this.query).then(result => {
        this.dataList = result.data.list; //返回数据结构参见API文档
        this.totalCount = result.data.totalCount;
        // 转换日期格式
        this.dataList.forEach(item => {
          // item.shelf_time = dayjs(item.shelf_time).format('YYYY-MM-DD');
        });
      });
    },

    // 触发搜索按钮
    handleSearch() {
      this.query.pasgeIndex = 1;
      this.loadDataList();
    },

    // 增加记录
    handleAdd() {
      this.editDialogVisible = true;
      this.$nextTick(() => {
        this.$refs.editDialog.init(0); //id==0表示新增
      });
    },

    // 触发导出按钮
    handleExport() {
      this.$message('导出，待完成...');
    },

    // 删除操作
    handleDelete(index, id) {
      // 二次确认删除
      this.$confirm('确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 选择了确认，则删除后端数据库中的记录
          GoodsAPI.deleteById(id)
            .then(() => {
              this.dataList.splice(index, 1); //同步列表中的记录
              this.$message.success('删除成功');
            })
            .catch(() => {
            });
        })
        .catch(() => {
        });
    },

    // 编辑操作，id为编辑记录的ID
    handleUpdate(id) {
      this.editDialogVisible = true;
      this.$nextTick(() => {
        this.$refs.editDialog.init(id);
      });
    },

    // 分页导航=======================================
    handlePageChange(val) {
      this.query.pageIndex = val;
      this.loadDataList();
    },

  }
};
</script>

<style scoped></style>
